import { Injectable } from '@nestjs/common';
import { CreateGeographDto } from './dto/create-geograph.dto';
import { UpdateGeographDto } from './dto/update-geograph.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Geography } from './entities/geograph.entity';
import { Repository } from 'typeorm';

@Injectable()
export class GeographService {
  constructor(
    @InjectRepository(Geography)
    private readonly geographRepository: Repository<Geography>,
  ) {}

  create(createGeographDto: CreateGeographDto) {
    const geograph = this.geographRepository.create(createGeographDto);
    return this.geographRepository.save(geograph);
  }

  async skipFind(
    current: number,
    size: number,
  ): Promise<Array<Geography> | any> {
    const list = await this.geographRepository
      .createQueryBuilder('user')
      .skip((current - 1) * size)
      .take(size)
      .getManyAndCount();

    return {
      list: list[0],
      total: list[1],
      size,
      current,
      pages: Math.ceil(list[1] / size),
    };
  }

  async findAll(): Promise<Array<Geography | any>> {
    return this.geographRepository.find();
  }

  async findOne(id: number): Promise<Geography | any> {
    return this.geographRepository.find({ where: { id } });
  }

  update(updateGeographDto: UpdateGeographDto) {
    const geograph = this.geographRepository.create(updateGeographDto);
    return this.geographRepository.save(geograph);
  }

  remove(id: number) {
    return this.geographRepository.delete(id);
  }
}
